/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			mainwindow_pslots.cc
  \date			Aug 2011
  \author		TNick

  \brief		Contains the implementation for private slots in MainWindow class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include    <QFileDialog>
#include    <QMessageBox>
#include    <QWhatsThis>
#include    <QContextMenuEvent>
#include    <QMenu>
#include    <QClipboard>
#include    <QGraphicsPixmapItem>


#include	<data_store/pagemdl.h>

#include	<gui/mainwindow.h>
#include	"ui_mainwindow.h"
#include	"genopts.h"
#include    "filtopts.h"


extern QString      s_default_page;


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
void        MainWindow::slot_No_skin            (void)
{
	qApp->setStyleSheet(QString());
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_LoadStySh          (void)
{
	QFileDialog::Options options;
	QString selectedFilter;
	QString fileName = QFileDialog::getOpenFileName(
				this,
				tr("Select style sheet to load ..."),
				QString(),
				tr("All Files (*);;Style sheets (*.css)"),
				&selectedFilter,
				options);
	if (!fileName.isEmpty())
		{
			_loadStySh(fileName);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_LayDir            	(void)
{
	if (layoutDirection() == Qt::LeftToRight)
		qApp->setLayoutDirection(Qt::RightToLeft);
	else
		qApp->setLayoutDirection(Qt::LeftToRight);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_About            	(void)
{
	QMessageBox::about(this, tr("About app"),
					   tr("<p><b>sellins</b> is a small utility aimed at "
						  "scanning web resources and producing "
						  "comprehensible results.</p>"
						  "<p>On August 26, 1919, <a href=\"https://secure.wikimedia.org/wikipedia/en/wiki/Fannie_Sellins\">"
						  "Fannie Sellins</a> witnessed guards beating Joseph Starzelski, "
						  "a picketing miner, and when she intervened, she was shot and "
						  "killed by two bullets. Others said that she was attempting "
						  "to protect miners' children that were on scene."
						  "</p>"
						  "<p>The code was written by "
						  "<a href=\"mailto:nicu.tofan@gmail.com\">TNick</a> "
						  "based on the original idea of CWN - "
						  "<a href=\"http://www.cyberwarnews.info/\">"
						  "Cyber War News</a></p>"));
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_Help            	(void)
{
	QMessageBox::about(this, tr("Help"),
					   tr("<p>Help system is not implemented yet.</p>"
						  "<p>Just push the things with your mouse "
						  "and see what happens</p>"));
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_HelpCtx            (void)
{
	QWhatsThis::enterWhatsThisMode();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_FilteredChange		(int filt_on)
{
	if (filt_on == Qt::Unchecked)
		{
			pg_mdl->setDisplayMode(PageMdl::ALL);
		}
	else if (ui->ck_InvertFilter->checkState() == Qt::Unchecked)
		{
			pg_mdl->setDisplayMode(PageMdl::FILTERED);
		}
	else //  if (ui->ck_InvertFilter->checkState() == Qt::Checked)
		{
			pg_mdl->setDisplayMode(PageMdl::UNFILTERED);
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_FilterInOutChange	(int filt_on)
{
	if (pg_mdl->displayMode() == PageMdl::ALL)
		return;

	if (filt_on == Qt::Unchecked)
		{
			pg_mdl->setDisplayMode(PageMdl::FILTERED);
		}
	else
		{
			pg_mdl->setDisplayMode(PageMdl::UNFILTERED);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_UItplChange        (QAction* p_active)
{
	foreach(QAction* qa, ui->menuUITpl->actions())
		{
			qa->setChecked(false);
		}
	p_active->setChecked(true);
	qApp->setStyle(p_active->text());
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void        MainWindow::slot_SkinChange         (QAction* p_active)
{
	QList<QAction *> acls = ui->menuStySheet->actions();
	int i = acls.indexOf(p_active);
	if (i < 2) return;

	QDir	dir_ss(QCoreApplication::applicationFilePath());
	dir_ss.cd(QString("../StyleSheets"));
	if (dir_ss.exists())
		{
			_loadStySh(dir_ss.absoluteFilePath(p_active->text()));
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_PgLstSelChange (
	const QModelIndex & current, const QModelIndex & previous)
{
	Q_UNUSED(previous);

	PageData * crtit = static_cast<PageData * >(
				current.internalPointer());
	if ((current.isValid()) && (crtit != NULL))
		{
			try
			{
				ui->webView->pageAction(QWebPage::Stop)->trigger();

				ui->tx_Address->setText(crtit->URL());
				ui->webView->load(QUrl(crtit->URL()));

			}
			catch(...)
			{
				ui->webView->setHtml(s_default_page);
			}
		}
	else
		{
			ui->webView->setHtml(s_default_page);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_GenOpts        (void)
{
	GenOpts * go = new GenOpts(this);
	go->show();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_FiltOpts       (void)
{
	FiltOpts * fo = new FiltOpts(this);
	fo->show();
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void            MainWindow::slot_StartStop      (void)
{
	setPaused(!paused());
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_RefreshLst     (void)
{
	timerEvent();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstCtxMnu      ( const QPoint& pt_of_mnu )
{
	if (currentPageD() != NULL)
		{
			QMenu   ctx_mnu(this);

			ctx_mnu.addAction(ui->action_ShowInBrowser);
			ctx_mnu.addSeparator();
			ctx_mnu.addAction(ui->action_LstCopyAll);
			ctx_mnu.addAction(ui->action_LstCopyName);
			ctx_mnu.addAction(ui->action_LstCopyURL);
			ctx_mnu.addAction(ui->action_LstCopyDate);
			ctx_mnu.addSeparator();
			ctx_mnu.addAction(ui->action_LstDelete);
			ctx_mnu.addAction(ui->action_LstDeleteAll);
			ctx_mnu.addAction(ui->action_LstDeleteFIn);
			ctx_mnu.addAction(ui->action_LstDeleteFOut);


			ctx_mnu.setActiveAction(ui->action_ShowInBrowser);
			ctx_mnu.setDefaultAction(ui->action_ShowInBrowser);

			ctx_mnu.exec(QWidget::mapToGlobal(pt_of_mnu), ui->action_LstCopyAll);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstCopy        (void)
{
	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			QClipboard *clipboard = QApplication::clipboard();
			clipboard->setText(
						p_crt->displName() + "\t" +
						p_crt->URL() + "\t" +
						p_crt->dateOfRetrieval()
						);
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstCopyName    (void)
{

	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			QClipboard *clipboard = QApplication::clipboard();
			clipboard->setText(p_crt->displName());
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstCopyAddr    (void)
{

	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			QClipboard *clipboard = QApplication::clipboard();
			clipboard->setText(p_crt->URL());
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstCopyData    (void)
{

	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			QClipboard *clipboard = QApplication::clipboard();
			clipboard->setText(p_crt->dateOfRetrieval());
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstDel         (void)
{

	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			pg_mdl->deletePage(p_crt);
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstDelAll      (void)
{
	pg_mdl->deleteAll();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstDelFIn      (void)
{
	pg_mdl->deleteFiltIn();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstDelFOut      (void)
{
	pg_mdl->deleteFiltOut();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_ShowInBrowser    (void)
{
	PageData * p_crt = currentPageD();
	if (p_crt != NULL)
		{
			QDesktopServices::openUrl(QUrl(p_crt->URL()));
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_LstDblClk          (const QModelIndex & midx)
{

	if (midx.isValid())
		{
			PageData * p_crt =
					static_cast<PageData *>(midx.internalPointer());
			if (p_crt != NULL)
				{
					QDesktopServices::openUrl(QUrl(p_crt->URL()));
				}
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void            MainWindow::slot_Advance            (int i)
{
	Q_UNUSED(i);


	static int a = 0;
	static int b = 0;
	static int da = 1;
	static int db = 1;

	if (a > ui->gv_progress->width()-24)
		da = -1;
	else if (a <= -2)
		da = 1;
	if (b > ui->gv_progress->height()-24)
		db = -1;
	else if (b <= -2)
		db = 1;

	a = a + da;
	b = b + db;
	//qDebug() << " a = " << a << " b = " << b;
	mov_icon->setPos(a, b);
}
/* ========================================================================= */



/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
